home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CommToolbox (modified) / Sources / CFileTransfer.cp < prev    next >
Encoding:
Text File  |  1994-11-30  |  21.0 KB  |  1,043 lines  |  [TEXT/KAHL]

  1. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
  2.  
  3.     CFileTransfer.c
  4.     
  5.     CommToolbox file transfer class.
  6.     
  7.     SUPERCLASS = CBureaucrat.
  8.     
  9.     Original copyright © 1992-93 Romain Vignes. All rights reserved.
  10.     Modifications copyright © 1994-95 Ithran Einhorn. All rights reserved.
  11.     
  12. ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  13.  
  14.  
  15. #include <CommResources.h>                        /* Apple includes */
  16.  
  17. #include <CBartender.h>                            /* TCL includes */
  18. #include <CCluster.h>
  19. #include <CError.h>
  20. #include <Constants.h>
  21. #include <TBUtilities.h>
  22. #include <TCLUtilities.h>
  23.  
  24. #include "CTermPane.h"                            /* Other includes */
  25. #include "CFileTransfer.h"
  26.  
  27. /* Constants & Macros */
  28.  
  29. #define FTRANS_STR_RES_ID    2300    /* Transfer messages resource ID */
  30.  
  31. #define NO_TOOL_STR_INDEX    1        /* No transfer tool */
  32. #define BAD_TOOL_STR_INDEX    2        /* Bad transfer tool */
  33. #define NO_REC_STR_INDEX    3        /* Transfer record allocation error */
  34. #define CHOOSE_STR_INDEX    4        /* tool setup error */
  35. #define START_ERR_STR_INDEX    5        /* transfer start error */
  36. #define SUCCESS_STR_INDEX    6        /* Transfer is successful */
  37. #define RUN_ERR_STR_INDEX    7        /* Transfer failed */
  38.  
  39. #define H_CHOOSE_POS        10        /* Setup dialog position */
  40. #define V_CHOOSE_POS        40
  41.  
  42. #define TEXT_FILE_TYPE        'TEXT'
  43.  
  44.  
  45. /* Application globals */
  46.  
  47. extern CBartender    *gBartender;
  48. extern CError        *gError;
  49.  
  50.  
  51. /* Class variables initialization */
  52.  
  53. CCluster    *CFileTransfer::cFTransList = NULL;
  54.  
  55.  
  56. /*
  57.  * cIsFileTransferCmd
  58.  *
  59.  * File transfer related command ?
  60.  *
  61.  * theCmd:    the command to be analysed
  62.  *
  63.  * Return TRUE if the command is file transfer related
  64.  *
  65.  */
  66.  
  67. Boolean CFileTransfer::cIsFileTransferCmd(long theCmd)
  68. {
  69.     return ((theCmd >= cmdFTransChoose) && (theCmd <= cmdFTransRecv));
  70. }
  71.  
  72.  
  73. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  74.  
  75.  
  76. /*
  77.  * cInitManager
  78.  *
  79.  * File Transfer Manager Initialization
  80.  *
  81.  */
  82.  
  83. void CFileTransfer::cInitManager(void)
  84. {
  85.     InitFT();
  86. }    
  87.  
  88.  
  89. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  90.  
  91. /*
  92.  * cGetCMVersion
  93.  *
  94.  * return the version of the File Transfer Manager
  95.  *
  96.  */
  97.  
  98. short CFileTransfer::cGetFTVersion(void)
  99. {
  100.     return FTGetFTVersion();
  101. }    
  102.  
  103.  
  104. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  105.  
  106. /*
  107.  * cCheckToolName
  108.  *
  109.  * Checking of the tool existence
  110.  *
  111.  * toolName:    name of the tool (pascal string)
  112.  *
  113.  * Return an error code
  114.  *
  115.  */
  116.  
  117. OSErr CFileTransfer::cCheckToolName(Str31 toolName)
  118. {
  119.     return(FTGetProcID(toolName));
  120. }    
  121.  
  122.  
  123. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  124.  
  125. /*
  126.  * cFTransIdle
  127.  *
  128.  * Idle time for each file transfer object
  129.  *
  130.  *
  131.  */
  132.  
  133.          /* Idle routine for each file transfer object */
  134.  
  135.         static void    FTrans_Idle(CFileTransfer *theFTrans)
  136.         {
  137.             theFTrans->DoIdle();
  138.         }
  139.  
  140.  
  141. void CFileTransfer::cFTransIdle(void)
  142. {
  143.     if (cFTransList != NULL)        /* List exists ? */
  144.         cFTransList->DoForEach((EachFunc) FTrans_Idle);
  145. }    
  146.  
  147.  
  148. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  149.  
  150. /*
  151.  * cTestToolMenu
  152.  *
  153.  * Test if the selected menu belongs to a file transfer tool
  154.  *
  155.  * theMenu:    selected menu ID
  156.  * theItem:    selected item ID
  157.  *
  158.  * Return TRUE if the menu is a file transfer tool menu
  159.  */
  160.  
  161.  
  162.          /* Test routine */
  163.  
  164.         static Boolean FTransTest(CFileTransfer *theFTrans)
  165.         {
  166.             return theFTrans->active;
  167.         }
  168.  
  169.  
  170. Boolean CFileTransfer::cTestToolMenu(short theMenu, short theItem)
  171. {
  172.     CFileTransfer    *current;
  173.     
  174.     if (cFTransList == NULL)
  175.         return FALSE;
  176.     else    {
  177.         current = (CFileTransfer *) cFTransList->FindItem((TestFunc) FTransTest);
  178.         
  179.         if (current == NULL)
  180.             return FALSE;
  181.         else
  182.             return current->DoMenu(theMenu,theItem);
  183.     }
  184. }    
  185.  
  186.  
  187. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  188.  
  189. /*
  190.  * cTestToolEvent
  191.  *
  192.  * Test if the event is related to a file transfer tool
  193.  *
  194.  * macEvent:    pointer on the event record
  195.  * theWindow:    pointer on the window record
  196.  *
  197.  * Return TRUE if the event is a file transfer tool event
  198.  */
  199.  
  200. typedef struct    {
  201.     EventRecord    *theEvent;
  202.     WindowPtr    theWindow;
  203.     Boolean        isToolEvent;
  204. } TestParamRec;
  205.  
  206.  
  207.          /* Test routine */
  208.  
  209.         static void FTransEvtTest(CFileTransfer *theFTrans,TestParamRec *params)
  210.         {
  211.             if (params->isToolEvent == FALSE)
  212.                 params->isToolEvent = theFTrans->DoEvent(params->theEvent,
  213.                     params->theWindow);
  214.         }
  215.  
  216.  
  217. Boolean CFileTransfer::cTestToolEvent(EventRecord *macEvent,WindowPtr theWindow)
  218. {
  219.     TestParamRec params;
  220.     
  221.     params.theEvent = macEvent;
  222.     params.theWindow = theWindow;
  223.     params.isToolEvent = FALSE;
  224.     
  225.     if (cFTransList == NULL)
  226.         return FALSE;
  227.     else    {
  228.         cFTransList->DoForEach1((EachFunc1) FTransEvtTest,(long) ¶ms);
  229.         
  230.         return params.isToolEvent;
  231.     }
  232. }    
  233.  
  234.  
  235. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  236.  
  237. /*
  238.  * IFileTransfer
  239.  *
  240.  * File transfer object initialization
  241.  *
  242.  * aSupervisor:            Supervisor in the chain of commands
  243.  * toolName:            Name of tool to use ("" -> default)
  244.  * flags:(FT)            Tool use flag
  245.  * sendProc:(FT)        Chars sending proc
  246.  * recvProc:(FT)        Chars receiving proc
  247.  * readProc:(FT)        Chars reading proc
  248.  * writeProc:(FT)        Chars writing proc
  249.  * environsProc:(FT)    Environment description proc
  250.  * refcon:(FT)            available for the application
  251.  * userData:(FT)        available for the application
  252.  *
  253.  * Parameters followed by FT are exact required parameters for creating a
  254.  * file transfer record.
  255.  *
  256.  */
  257.  
  258. void CFileTransfer::IFileTransfer(CBureaucrat *aSupervisor,Str31 toolName,
  259.                         FTFlags flags,FTSendProcPtr sendProc,FTRcveProcPtr recvProc,
  260.                         FTReadProcPtr readProc,FTWriteProcPtr writeProc,FTEnvProcPtr environsProc,
  261.                         WindowPtr owner,long refCon, long userData)
  262. {
  263.     FTHandle    theFTrans;
  264.     OSErr        theErr;
  265.     Str31        tName;
  266.     short        toolProcID;
  267.     Boolean        savedAlloc;
  268.     
  269.     CBureaucrat::IBureaucrat(aSupervisor);    /* Initialize its superclass */
  270.     
  271.     gStartFT = false;
  272.     gFTSearchRefNum = 0L;        // Auto-Initiate File Transfers    
  273.             
  274.     // if we belong to a terminal pane, get its connection
  275.     if (member(aSupervisor, CTermPane))
  276.         BindConnection(((CTermPane *)aSupervisor)->GetConnection());
  277.     
  278.     if (toolName[0] == 0)        /* Default tool ? */
  279.     {
  280.         theErr = CRMGetIndToolName(classFT,1,tName);
  281.         
  282.         if ((theErr != ftNoErr) || (tName[0] == 0))         /* No tool */
  283.             Failure(ftNoTools,SpecifyMsg(FTRANS_STR_RES_ID,NO_TOOL_STR_INDEX));
  284.  
  285.         toolProcID = cCheckToolName(tName);            /* Default tool ID */
  286.     }
  287.     else
  288.         toolProcID = cCheckToolName(toolName);            /* Chosen tool ID */
  289.     
  290.     if (toolProcID == ftGenericError)            /* No tool */
  291.             Failure(ftNoTools,SpecifyMsg(FTRANS_STR_RES_ID,BAD_TOOL_STR_INDEX));
  292.  
  293.     if (sendProc == nil)
  294.         sendProc = (FTSendProcPtr)DefSendProc;
  295.  
  296.     if (recvProc == nil)
  297.         recvProc = DefReceiveProc;
  298.         
  299.     if (environsProc == nil)
  300.         environsProc = (FTEnvProcPtr)DefGetFTEnvirons;
  301.         
  302.     savedAlloc = SetAllocation(kAllocCanFail);
  303.     
  304.     theFTrans = FTNew(toolProcID,flags,sendProc,recvProc,readProc,writeProc,
  305.                     environsProc,owner,(long)this,userData);
  306.                     
  307.     SetAllocation(savedAlloc);
  308.     
  309.     FailNIL(theFTrans);                /* File transfer record created ? */
  310.  
  311.     MoveHHi((Handle)theFTrans);        /* Heap fragmentation… */
  312.  
  313.     itsFTrans = theFTrans;            /* Instance variable */
  314.     
  315.     if (cFTransList == NULL)    {    /* FIrst transfer object ? */
  316.         cFTransList = new(CCluster);
  317.         cFTransList->ICluster();
  318.     }
  319.         
  320.     cFTransList->Add(this);         /* Transfer addition */
  321.         
  322.     this->wasFTMode = FALSE;    
  323.     this->active = FALSE;
  324.     
  325.     AddSearch();                    /* allow for auto-downloads */
  326.     
  327.     #ifdef __USETHREADS__
  328.     if (CThread::cIsPresent())
  329.     {
  330.         itsIdleThread = new(CThread);
  331.         itsIdleThread->IThread(kCooperativeThread, (ThreadEntryProcPtr)cOneFTIdle, this);
  332.     }
  333.     #endif
  334. }
  335.  
  336.  
  337. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  338.  
  339. /*
  340.  * Dispose
  341.  *
  342.  * File transfer object removal
  343.  *
  344.  */
  345.  
  346. void CFileTransfer::Dispose(void)
  347. {
  348.     ASSERT(cFTransList != NULL);
  349.     
  350.     #ifdef __USETHREADS__
  351.     if (CThread::cIsPresent())
  352.     {
  353.         itsIdleThread->Dispose();
  354.     }
  355.     #endif
  356.     
  357.     RemoveSearch();                    /* tell connection tool to forget about auto-downloads */
  358.     
  359.     cFTransList->Remove(this);        /* Dispose of the file transfer */
  360.     
  361.     if (cFTransList->IsEmpty())        
  362.         ForgetObject(cFTransList);    /* Dispose of the cluster */
  363.  
  364.     FTDispose(itsFTrans);            /* Transfer record disposal */
  365.     itsFTrans = NULL;
  366.     
  367.     inherited::Dispose();            /* Pass message to its superclass */
  368. }
  369.  
  370.  
  371. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  372.  
  373. /*
  374.  * UpdateMenus
  375.  *
  376.  * Transfer related menus updating
  377.  *
  378.  */
  379.  
  380. void CFileTransfer::UpdateMenus(void)
  381. {
  382.     if (!IsRunning())    {
  383.     
  384.         gBartender->EnableCmd(cmdFTransChoose);        /* Setup command */
  385.         
  386.         if (!((*itsFTrans)->attributes & ftSendDisable))
  387.             gBartender->EnableCmd(cmdFTransSend);    /* Send command */
  388.     
  389.         if (!((*itsFTrans)->attributes & ftReceiveDisable))
  390.             gBartender->EnableCmd(cmdFTransRecv);    /* Receive command */
  391.     
  392.     }
  393. }
  394.  
  395.  
  396. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  397.  
  398. /*
  399.  * DoCommand
  400.  *
  401.  * Handle file transfer commands
  402.  *
  403.  * theCommand:    command to be executed
  404.  *
  405.  */
  406.  
  407. void CFileTransfer::DoCommand(long theCommand)
  408. {
  409.     switch (theCommand)    {        
  410.     
  411.         case cmdFTransChoose:        /* Transfer tool setup */
  412.             this->FileTransferChoose();
  413.             break;
  414.             
  415.         case cmdFTransSend:        /* File sending */
  416.             this->Start(ftTransmitting);
  417.             break;
  418.             
  419.         case cmdFTransRecv:        /* File receiving */
  420.             this->Start(ftReceiving);
  421.             break;
  422.             
  423.         default:                /* Unknown command */
  424.             break;
  425.     }
  426. }
  427.  
  428.  
  429. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  430.  
  431. /*
  432.  * FileTransferChoose
  433.  *
  434.  * Transfer tool setup
  435.  *
  436.  */
  437.  
  438. void CFileTransfer::FileTransferChoose(void)
  439. {
  440.     short        retCode;
  441.     Point        where;
  442.     FTHandle    hFTrans;
  443.     
  444.     hFTrans = this->itsFTrans;
  445.     HUnlock((Handle)hFTrans);
  446.     
  447.     SetPt(&where,H_CHOOSE_POS,V_CHOOSE_POS);    /* Dialog position */
  448.                                                 
  449.     retCode = FTChoose(&hFTrans,where,NULL);
  450.     
  451.     HLock((Handle)hFTrans);
  452.     this->itsFTrans = hFTrans;
  453.  
  454.     switch (retCode)    {        
  455.         case chooseCancel:        /* Setup cancelling */
  456.             break;
  457.             
  458.         case chooseOKMinor:        /* Same tool, changed config */
  459.         case chooseOKMajor:        /* Changed tool */
  460.         
  461.             RemoveSearch();        // reset the auto-download search pattern.
  462.             AddSearch();
  463.         
  464.             active = FALSE;
  465.             this->Activate();                /* Activation */
  466.         
  467.             itsSupervisor->Notify(NULL);     /* Notify document update */
  468.             
  469.             break;
  470.             
  471.         default:                /* Unknown code */
  472.             SysBeep(3);
  473.             gError->PostAlert(FTRANS_STR_RES_ID,CHOOSE_STR_INDEX);
  474.             break;    
  475.     }
  476. }
  477.  
  478.  
  479. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  480.  
  481. /*
  482.  * SetConfig
  483.  *
  484.  * Modify file transfer tool config
  485.  *
  486.  * theConfig:    configuration string (C string))
  487.  *
  488.  * Renvoie:        negative value: error (-1 -> unknown)
  489.  *                positive value: parser stop index
  490.  *                cmNoErr if all is OK
  491.  *
  492.  */
  493.  
  494. short CFileTransfer::SetConfig(char *theConfig)
  495. {
  496.     short retCode;
  497.     
  498.     retCode = FTSetConfig(itsFTrans,theConfig);
  499.     
  500.     if (retCode == ftNoErr)
  501.     {
  502.         RemoveSearch();        // reset the auto-download search pattern.
  503.         AddSearch();
  504.     }
  505.     
  506.     return retCode;
  507. }    
  508.  
  509.  
  510. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  511.  
  512. /*
  513.  * GetToolName
  514.  *
  515.  * Return the file transfer tool name
  516.  *
  517.  * toolName:    Name of the tool
  518.  *
  519.  */
  520.  
  521. void CFileTransfer::GetToolName(Str31 toolName)
  522. {
  523.     SignedByte    savedState;
  524.     
  525.     savedState = HGetState((Handle)itsFTrans);
  526.     HLock((Handle)itsFTrans);
  527.     
  528.     FTGetToolName((*itsFTrans)->procID,toolName);
  529.     
  530.     HSetState((Handle)itsFTrans,savedState);
  531. }    
  532.  
  533.  
  534. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  535.  
  536. /*
  537.  * GetConfig
  538.  *
  539.  * Return the config string of the tool
  540.  *
  541.  * Return a pointer on a C string
  542.  *
  543.  */
  544.  
  545. Ptr CFileTransfer::GetConfig(void)
  546. {
  547.     return(FTGetConfig(itsFTrans));
  548. }    
  549.  
  550.  
  551. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  552.  
  553. /*
  554.  * DoIdle
  555.  *
  556.  * Idle time of the application
  557.  *
  558.  */
  559.  
  560. void CFileTransfer::DoIdle(void)
  561. {
  562.  
  563.     if (IsRunning())
  564.     {
  565.         FTExec(itsFTrans);                        /* Transfer tool handles these events */
  566.     }
  567.     else if (this->wasFTMode)
  568.     {
  569.         /* Running ? */
  570.                 
  571.         this->wasFTMode = FALSE;                /* Transfer halted */
  572.         
  573.         AddSearch();                            /* allow for searching for auto-download pattern. */
  574.     }
  575.     else if (AutoDownload ())
  576.     {
  577.         Start(ftReceiving);
  578.     }
  579.         
  580. }                                        
  581.  
  582. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  583.  
  584. /*
  585.  * cOneFTIdle
  586.  * 
  587.  * terminal idle time
  588.  *
  589.  */
  590.  
  591. pascal void    *CFileTransfer::cOneFTIdle (void *threadParam)
  592. {
  593.     CFileTransfer *anFT = (CFileTransfer *)threadParam;
  594.     
  595.     #ifdef __USETHREADS__
  596.     for (;;)
  597.     {
  598.         ThreadBeginCritical();
  599.     #endif
  600.  
  601.         anFT->DoIdle();    
  602.  
  603.     #ifdef __USETHREADS__
  604.         ThreadEndCritical();
  605.         YieldToAnyThread();
  606.     }
  607.     #endif
  608.  
  609.     return nil;
  610. }
  611.  
  612. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  613.  
  614. /*
  615.  * Activate
  616.  *
  617.  * Transfer activation
  618.  *
  619.  */
  620.  
  621. void CFileTransfer::Activate(void)
  622. {
  623.     if (!active)    {
  624.         FTActivate(itsFTrans,TRUE);
  625.         active = TRUE;
  626.     }
  627. }                                        
  628.  
  629.  
  630. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  631.  
  632. /*
  633.  * Deactivate
  634.  *
  635.  * Transfert deactivation
  636.  *
  637.  */
  638.  
  639. void CFileTransfer::Deactivate(void)
  640. {
  641.     if (active)    {
  642.         FTActivate(itsFTrans,FALSE);
  643.         active = FALSE;
  644.     }
  645. }                                        
  646.  
  647.  
  648. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  649.  
  650. /*
  651.  * IsRunning
  652.  *
  653.  * Return the state of the file transfer
  654.  *
  655.  * Return TRUE if the transfer is running
  656.  *
  657.  */
  658.  
  659. Boolean CFileTransfer::IsRunning(void)
  660. {    
  661.     return ((*itsFTrans)->flags & ftIsFTMode);
  662. }                                        
  663.  
  664.  
  665. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  666.  
  667. /*
  668.  * IsRunningOnSameCircuit
  669.  *
  670.  * Return the state of the file transfer
  671.  *
  672.  * Return TRUE if the transfer is running on same circuit
  673.  *
  674.  */
  675.  
  676. Boolean CFileTransfer::IsRunningOnSameCircuit(void)
  677. {    
  678.     return (IsRunning() && ((*itsFTrans)->attributes & ftSameCircuit));
  679. }
  680.  
  681. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  682.  
  683. /*
  684.  * NeedsConnection
  685.  *
  686.  * Return the state of the file transfer
  687.  *
  688.  * Return TRUE if the transfer wants the connection
  689.  *
  690.  */
  691.  
  692. Boolean CFileTransfer::NeedsConnection(void)
  693. {    
  694.     return IsRunning() || AutoDownload() || wasFTMode;
  695. }                                        
  696.  
  697. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  698.  
  699. /*
  700.  * Start
  701.  *
  702.  * File transfer startup
  703.  *
  704.  * direction:    transfer direction
  705.  *
  706.  */
  707.  
  708. void CFileTransfer::Start(FTDirection direction)
  709. {
  710.     SFReply        theReply;
  711.     Point        corner;
  712.     short        numTypes;
  713.     SFTypeList    typeList;
  714.     OSErr        theErr;
  715.     
  716.     switch (direction)    {        
  717.     
  718.         case ftTransmitting:    /* Sending */
  719.         
  720.             if ((*itsFTrans)->attributes & ftTextOnly)    { /* Text files only */
  721.                 typeList[0] = TEXT_FILE_TYPE;
  722.                 numTypes = 1;
  723.             }
  724.             else
  725.                 numTypes = -1;            /* All types of file */
  726.                 
  727.             FindDlogPosition('DLOG', getDlgID, &corner);
  728.     
  729.             SFPGetFile(corner,"\p", NULL, numTypes, typeList,
  730.                         NULL,&theReply, getDlgID, NULL);
  731.                         
  732.             if (theReply.good)    {    /* Validate */
  733.             
  734.                 theErr = FTStart(itsFTrans,ftTransmitting,&theReply);
  735.                 
  736.                 if (theErr == ftNoErr)
  737.                 {    
  738.                     /* Erreur de lancement */
  739.                     this->wasFTMode = TRUE;
  740.                 }
  741.             }
  742.         
  743.             break;
  744.             
  745.         case ftReceiving:        /* Receiving */
  746.         
  747.             // We remove the search temporarily in case it comes        
  748.             // across during the transfer. Will be re-added in the    
  749.             // idle loop once the transfer is completed                
  750.             
  751.             RemoveSearch();
  752.             
  753.             theReply.vRefNum = 0;
  754.             theReply.fName[0] = 0;
  755.             theReply.good = TRUE;
  756.         
  757.             theErr = FTStart(itsFTrans,ftReceiving,&theReply);
  758.             
  759.             if (theErr == ftNoErr)
  760.             {    
  761.                 /* Erreur de lancement */
  762.                 this->wasFTMode = TRUE;                
  763.             }
  764.             break;
  765.             
  766.         default:                /* Other */
  767.             break;
  768.     }
  769. }                                        
  770.  
  771.  
  772. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  773.  
  774. /*
  775.  * DoEvent
  776.  *
  777.  * Transfer related event handling
  778.  *
  779.  * theEvent:    Pointer on the event record
  780.  * theWindow:    Window in which occured the event
  781.  *
  782.  * Renvoie TRUE if the event is handled by the tool
  783.  *
  784.  */
  785.  
  786. Boolean CFileTransfer::DoEvent(EventRecord *theEvent,WindowPtr theWindow)
  787. {
  788.     Boolean    isToolEvent;
  789.     FTHandle    ftHdl;
  790.     
  791.     isToolEvent = FALSE;
  792.     
  793.     ftHdl = (FTHandle) GetWRefCon(theWindow);
  794.     
  795.     if (ftHdl == itsFTrans)    {    /* Tool window ? */
  796.         FTEvent(itsFTrans,theEvent);
  797.         isToolEvent = TRUE;
  798.     }
  799.     
  800.     return isToolEvent;
  801. }    
  802.  
  803.  
  804. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  805.  
  806. /*
  807.  * Success
  808.  *
  809.  * Get the ending status of the file transfer
  810.  *
  811.  * Renvoie TRUE if transfer was successfull
  812.  *
  813.  */
  814.  
  815. Boolean CFileTransfer::Success(void)
  816. {
  817.     return ((*itsFTrans)->flags & ftSucc);
  818. }                                        
  819.  
  820.  
  821. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  822.  
  823. /*
  824.  * DoMenu
  825.  *
  826.  * Handle tool menus
  827.  *
  828.  * theMenu:    selected menu
  829.  * theItem:    selected item
  830.  *
  831.  * Return TRUE if the menu belongs to the tool
  832.  *
  833.  */
  834.  
  835. Boolean CFileTransfer::DoMenu(short theMenu,short theItem)
  836. {
  837.     return(FTMenu(itsFTrans,theMenu,theItem));
  838. }    
  839.  
  840. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  841.  
  842. /*
  843.  * GetFTHandle
  844.  *
  845.  * return the FT handle
  846.  *
  847.  */
  848.  
  849. FTHandle CFileTransfer::GetFTHandle (void)
  850. {
  851.  return itsFTrans;
  852. }
  853.     
  854. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  855.  
  856. /*
  857.  * BindConnection, GetConnection
  858.  *
  859.  * manage xfer's connection
  860.  *
  861.  */
  862.  
  863. void CFileTransfer::BindConnection(CConnection *aConnection)
  864. {
  865.  itsConnection = aConnection;
  866. }
  867.  
  868. CConnection *CFileTransfer::GetConnection(void)
  869. {
  870.  return itsConnection;
  871. }
  872.  
  873. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  874.  
  875. /*
  876.  * AddSearch
  877.  *
  878.  * Add auto download search string
  879.  *
  880.  */
  881.  
  882. void CFileTransfer::AddSearch (void)
  883. {
  884.     Str255    tempStr;    // the string to look for
  885.     
  886.     if ((itsFTrans != NULL) && (itsConnection)) 
  887.     {
  888.         if ((*itsFTrans)->autoRec[0]) // Do I need to add a search
  889.         {
  890.             SignedByte    savedState;
  891.         
  892.             savedState = HGetState((Handle)itsFTrans);
  893.             HLock((Handle)itsFTrans);
  894.             
  895.             BlockMove((*itsFTrans)->autoRec, tempStr, (*itsFTrans)->autoRec[0] + 1);
  896.                     
  897.             gFTSearchRefNum = itsConnection->AddSearch(tempStr, cmSearchSevenBit, nil);
  898.             
  899.             if (gFTSearchRefNum == -1) 
  900.             {
  901.                 gFTSearchRefNum = 0;
  902.             }
  903.             
  904.             HSetState((Handle)itsFTrans,savedState);
  905.             
  906.         } // can autoreceive    
  907.         
  908.     } // good FT and Conn    
  909.     
  910. }
  911.     
  912. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  913.  
  914. /*
  915.  * RemoveSearch
  916.  *
  917.  * Remove auto download search string
  918.  *
  919.  */
  920.  
  921. void CFileTransfer::RemoveSearch (void)
  922. {
  923.  gStartFT = false;
  924.  
  925.  if (itsConnection)
  926.      if (((*itsFTrans)->autoRec[0] != 0) && (gFTSearchRefNum != 0)) 
  927.      {
  928.         itsConnection->RemoveSearch(gFTSearchRefNum);
  929.         gFTSearchRefNum = 0;    // We found it already    
  930.      }
  931. }
  932.  
  933. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  934.  
  935. /*
  936.  * get/set instance variables
  937.  *
  938.  *
  939.  */
  940.  
  941. long CFileTransfer::GetSearchNum (void)
  942. {
  943.  return gFTSearchRefNum;
  944. }
  945. void CFileTransfer::SetSearchNum (long SearchNum)
  946. {
  947.  gFTSearchRefNum = SearchNum;
  948. }
  949.     
  950. Boolean    CFileTransfer::AutoDownload (void)
  951. {
  952.  return gStartFT;
  953. }
  954. void    CFileTransfer::SetAutoDownload (Boolean state)
  955. {
  956.  gStartFT = state;
  957. }
  958.     
  959. /*******************************************************************
  960. *    ToolGetConnEnvirons    - Gets the connection environs for
  961. *                        the FT or Term tool
  962. *
  963. *        refcon            - the tool refcon
  964. *        theEnvirons        - the environment
  965. *
  966. *        returns            - an environment error
  967. *
  968. **********************************************************************/
  969.  
  970. pascal OSErr CFileTransfer::DefGetFTEnvirons(long refCon, ConnEnvironRec *theEnvirons)
  971. {
  972.  CFileTransfer    *theFTPane = (CFileTransfer *)refCon;
  973.  CConnection    *theConnection = theFTPane->GetConnection();
  974.  
  975.  return theConnection->GetEnvirons(theEnvirons);
  976. }
  977.  
  978. /*******************************************************************
  979. *    FTSendProc    - Sends data during a file transfer
  980. *
  981. *        thePtr        - data to send
  982. *        theSize        - bytes to send
  983. *        refcon        - the FTtool refcon
  984. *        channel        - which channel to use
  985. *        flags        - connection flags
  986. *
  987. *        returns        - bytes sent
  988. *
  989. **********************************************************************/
  990.  
  991. pascal long CFileTransfer::DefSendProc(Ptr thePtr,long theSize,long refCon,CMChannel channel,short flags)
  992. {
  993.     CMErr            theErr;
  994.     long            retCode = 0;                    // Assume the worst        
  995.     CFileTransfer    *theFTPane = (CFileTransfer *)refCon;
  996.     CConnection        *theConnection = theFTPane->GetConnection();
  997.     
  998.     if (theConnection->GetConnHandle())
  999.     {
  1000.         // Send the data            
  1001.         theErr = theConnection->DataWrite(thePtr,&theSize,channel,nil,15,flags);
  1002.         if (theErr == noErr)
  1003.             retCode = theSize;        // if ok, we sent all    
  1004.  
  1005.     } // Good Connection    
  1006.     
  1007.     return retCode;
  1008. }
  1009.  
  1010. /*******************************************************************
  1011. *    FTReceiveProc    - Gets data during a file transfer
  1012. *
  1013. *        thePtr        - place for data
  1014. *        theSize        - bytes to get
  1015. *        refcon        - the FTtool refcon
  1016. *        channel        - which channel to use
  1017. *        flags        - connection flags
  1018. *
  1019. *        returns        - bytes gotten
  1020. *
  1021. **********************************************************************/
  1022.  
  1023. pascal long CFileTransfer::DefReceiveProc (Ptr thePtr,long theSize,long refCon,CMChannel channel,CMFlags *flags)
  1024. {
  1025.     CMErr            theErr;
  1026.     long            retCode = 0;                    // Assume the worst
  1027.     CFileTransfer    *theFTPane = (CFileTransfer *)refCon;
  1028.     CConnection        *theConnection = theFTPane->GetConnection();
  1029.     
  1030.     if (theConnection->GetConnHandle())
  1031.     {
  1032.         // Read all the data    
  1033.         theErr = theConnection->DataRead (thePtr, &theSize, FALSE, NULL, 0L, flags, channel);
  1034.         if (theErr == noErr)
  1035.             retCode = theSize;    // if ok, we got all        
  1036.  
  1037.     } // Good Connection    
  1038.     
  1039.     return retCode;
  1040. }
  1041.  
  1042. /* ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ */
  1043.